package com.game.animation;

import com.game.config.Config;


/**
* @author oven:
* @version 创建时间：2012-11-6 上午8:54:38
* 
*/
public abstract class Animation {
	public static final int ANIMATION_TYPE_TRANSLATE=0;
	public static final int ANIMATION_TYPE_SCALE=1;
	public static final int ANIMATION_TYPE_ROATE=2;
	public static final int ANIMATION_TYPE_ALPHA=3;
	
	//动画组标记,同组的可同时执行
	public int group = -1;
	public Animation next=null;
	//执行完调用的方法
	public AnimationCallBack animationCallBack;
	//动画播放的总时间
	protected float time;
	//动画已播放的时间
	protected float time_step=0;
	protected AnimationFix animationfix;
	//动画执行完是否保存状态
	protected boolean keep=false;
	//延迟执行的时间
	protected float delayTime=0;
	//动画类型
	protected int animationType;
	//循环
	protected boolean loop=false;
	
	public Animation(AnimationFix animationfix,AnimationCallBack animationCallBack,float delayTime,boolean keep,float time){
		this.animationfix=animationfix;
		this.animationCallBack=animationCallBack;
		this.keep=keep;
		//由于是用时间来计算,现实世界和游戏世界时间为 1000:Config.FPS,把现实世界时间转换为游戏时间
		this.delayTime=delayTime*Config.FPS/1000f;
		time=time*Config.FPS/1000f;
		this.time=time;
	}
	
	/**
	 * 添加一个动画
	 * @param animation
	 */
	public void addAnimation(Animation animation){
		if(next==null){
			next=animation;
		}else{
			next.addAnimation(animation);
		}
	}
	
	public Animation stepAnimation(){
		return stepAnimation(-2);
	}
	
	protected Animation stepAnimation(int group){
		Animation result = this;
		boolean stepAble=false;
		//第一个animation
		if(group==-2){
			stepAble=true;
		}else{
			//同组 同时迭代
			if(this.group==group&&group!=-1){
				stepAble=true;
			}
		}
		if(stepAble){
			boolean endFlag=false;
			if(delayTime>0){
				delayTime--;
			}else{
				step();
				if(time_step>=time){
					if(loop){
						time_step=0;
					}else{
						endFlag=true;
					}
				}
				time_step++;
			}
			if(next!=null&&next.animationType!=this.animationType){
				next=next.stepAnimation(this.group);
			}
			if(endFlag){
				if(keep){
					keepState();
				}
				//重置
				switch (animationType) {
					case ANIMATION_TYPE_TRANSLATE:{
						animationfix.resetTransform();
						break;
					}
					case ANIMATION_TYPE_SCALE:{
						animationfix.resetScale();
						break;
					}
				}
				//指向下一个animation
				result=next;
				//调用callBack
				if(animationCallBack!=null){
					animationCallBack.CallBack(this);
				}
			}
		}
		return result;
	}
	
	protected abstract void step();
	
	protected abstract void keepState();

	public int getGroup() {
		return group;
	}

	public void setGroup(int group) {
		this.group = group;
	}

	public boolean isLoop() {
		return loop;
	}

	public void setLoop(boolean loop) {
		this.loop = loop;
	}
}



